<?php
defined('_JEXEC') or die('Restricted access');
jimport('joomla.application.component.model');
class ComplaintsModelcomment extends JModel
{
	var $_data;
	var $_pagination = null;
	var $_total = null;
	function __construct() {
		parent::__construct();
		global $mainframe, $option;
		$limit = $mainframe->getUserStateFromRequest('global.list.limit','limit',$mainframe->getCfg('list_limit'), 'int');
		$limitstart = JRequest::getVar('limitstart', 0, '', 'int');
		$limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);
		$limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);
		$this->setState('limit', $limit);
		$this->setState('limitstart', $limitstart);
	}
	function setId($id) {
		$this->_id = $id;
		$this->_data = null;
	}
	function _buildQuery() {
		$query = 'SELECT * FROM #__Complaints_comment';
		return $query;
	}
	function getPagination() {
		if (empty($this->_pagination)) {
		jimport('joomla.html.pagination');
		$this->_pagination = new JPagination($this->getTotal(),$this->getState('limitstart'),$this->getState('limit'));
	}
		return $this->_pagination;
	}
	function getData() {
		if (empty($this->_data)) {
		$query = $this->_buildQuery();
		$this->_data = $this->_getList($query, $this->getState('limitstart'), $this->getState('limit'));
		}
		return $this->_data;
	}
	function getTotal() {
		if (empty($this->_total)) {
		$query = $this->_buildQuery();
		$this->_total = $this->_getListCount($query);
		}
		return $this->_total;
	}
	function delete($cid) {
		if (!$cid) return false;
		else {
		$table = $this->getTable();
		if (!$table->delete($cid)) {
		return false;
		}
		return true;
		}
	}
	function &getComment()
	{
		if ($this->_loadComment()) {
		} else $this->_initComment();

		return $this->_comment;
	}

	function _loadComment()
	{
		if (empty($this->_comment))
		{
			$query = 'SELECT * FROM #__Complaints_comment WHERE id='.$this->_id;
			$this->_db->setQuery($query);
			$this->_comment = $this->_db->loadObject();
		}
	}

	function _initComment()
	{
		if (empty($this->_comment)) {
			$comment = new stdClass();
			$comment->id = 0;
			$comment->complaint_id = null;
			$comment->parent_id = null;
			$comment->comment_content = null;
			$comment->userid = null;
			$this->_comment = comment;
			return (boolean) $this->_comment;
		}
		return true;
	}
	function store() {
		$row =& JTable::getInstance('comment', 'Table');
		$id = JRequest::getString('comment_id');
	$complaint_id = JRequest::getString('comment_complaint_id');
	$parent_id = JRequest::getString('comment_parent_id');
	$comment_content = JRequest::getString('comment_comment_content');
	$userid = JRequest::getString('comment_userid');
		if ($id) $row->set('id', $id);
			$row->set('complaint_id', $complaint_id);
			$row->set('parent_id', $parent_id);
			$row->set('comment_content', $comment_content);
			$row->set('userid', $userid);
		if (!$row->check()) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}
		if (!$row->store()) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}
	return true;
	}
}
?>
